// pages/shop/shop.js
import Shopping from "./model.js";
let obj = new Shopping();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    totalPrice: '0',
    num: '0',
  },
  change: function (e) {
    var that = this;
    const index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    var goods = that.data.goods; // 获取购物车列表
    var selectAllStatus = that.data.selectAllStatus; //获取全选状态
    const selected = goods[index].selected; // 获取当前商品的选中状态
    goods[index].selected = !selected; // 改变状态
    goods[index]['selected'] = !selected;
    //判断有一个没有选中，全选取消
    var j = 0;
    for (var i = 0; i < goods.length; i++) {
      if (goods[i].selected == true) {
        j++;
        continue;
      } else {
        selectAllStatus = false;
      }
    }
    if (j == goods.length) {
      selectAllStatus = true;
    }
    //如果都选中，全选也选中实现
    that.setData({
      goods: goods,
      selectAllStatus: selectAllStatus,
    });
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  // 全选事件
  selectAll: function (e) {
    var that = this;
    var selectAllStatus = that.data.selectAllStatus; // 是否全选状态
    selectAllStatus = !selectAllStatus;
    var goods = that.data.goods;
    for (var i = 0; i < goods.length; i++) {
      goods[i].selected = selectAllStatus;
      goods[i]['selected'] = selectAllStatus; // 改变所有商品状态
    }
    that.setData({
      selectAllStatus: selectAllStatus,
      goods: goods
    });
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //加法
  addtion: function (e) {
    console.log(e)
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //默认99件最多
    if (num < 100) {
      num++
    }
    //把新的值给新的数组
    var newList = that.data.goods
    newList[index].num = num

    //把新的数组传给前台
    that.setData({
      goods: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //减法
  subtraction: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //把新的值给新的数组
    var newList = that.data.goods
    //当1件时，点击移除
    if (num == 1) {
      num == 1
    } else {
      num--
      newList[index].num = num
    }
    //把新的数组传给前台
    that.setData({
      goods: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //计算数量
  countNum: function () {
    var that = this
    //遍历数组，把既选中的num加起来
    var newList = that.data.goods
    var allNum = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].selected) {
        allNum += parseInt(newList[i].num)
      }
    }
    that.setData({
      num: allNum
    })
  },
  //计算金额方法
  count: function () {
    var that = this
    //选中的订单，数量*价格加起来
    var price = parseFloat(that.data.goods.price)
    var newList = that.data.goods
    var newCount = 0.00
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].selected) {
        newCount += newList[i].price * newList[i].num
      }
    }
    that.setData({
      // newCount: newCount.toFixed(2),
      totalPrice: newCount,
    })
  },
  toBuy() {
    wx.showToast({
      title: '结算成功',
      icon: 'success',
      duration: 3000,
    });
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    obj.getList(this);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})